package com.web.gobang.view;

import com.web.gobang.data.Player;
import com.web.gobang.game.GameCoupe;
import com.web.gobang.game.GameRobot;

import javax.swing.*;
import java.awt.*;

public class ChooseView extends JFrame {
    public static JButton btnCoupe, btnOnline, btnRobot, btnLogin, btnLogout;
    public static Player myPlayer = new Player(null,null);
    public static ChooseView chooseView;
    public static JPanel container;
    public static JLabel welcome;

    // 窗体大小
    private int width = 460;
    private int height = 360;
    //背景图片

    public ChooseView() {
        chooseView = this;
        //设置窗口位置在窗口中心
        int sWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        int sHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation((sWidth - width) / 2, (sHeight - height) / 2);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        chooseView.setTitle("联机五子棋");
        this.setSize(width, height);
        this.setResizable(false);

        //父容器
        container = new JPanel();
        container.setLayout(null);
        this.setContentPane(container);

        //title
        ImageIcon titleIcon = new ImageIcon("./img/title.png");
        JLabel title = new JLabel(titleIcon);
        title.setBounds(70,30,300,40);
        container.add(title);

        //背景
        JPanel imPanel=(JPanel) this.getContentPane();//注意内容面板必须强转为JPanel才可以实现下面的设置透明
        imPanel.setOpaque(false);//将内容面板设为透明
        ImageIcon icon=new ImageIcon("./img/loginBG.jpg");//背景图
        JLabel label = new JLabel(icon);//往一个标签中加入图片
        label.setBounds(0, 0, this.getWidth(), this.getHeight());//设置标签位置大小，记得大小要和窗口一样大
        icon.setImage(icon.getImage().getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH));//图片自适应标签大小
        this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));//标签添加到层面板

        //按钮
        ImageIcon buttonIcon = new ImageIcon("./img/buttonBG.png");
        buttonIcon.setImage(buttonIcon.getImage().getScaledInstance(200, 40, Image.SCALE_SMOOTH));

        btnCoupe = new JButton("双人对战",buttonIcon);
        btnCoupe.setHorizontalTextPosition(SwingConstants.CENTER);
        btnCoupe.setOpaque(false);
        btnCoupe.setContentAreaFilled(false);//设置图片填满按钮所在的区域
        btnCoupe.setBorderPainted(false);//设置是否绘制边框
        btnCoupe.setBorder(null);//设置边框


        btnRobot = new JButton("人机对战",buttonIcon);
        btnRobot.setHorizontalTextPosition(SwingConstants.CENTER);
        btnRobot.setOpaque(false);
        btnRobot.setContentAreaFilled(false);//设置图片填满按钮所在的区域
        btnRobot.setBorderPainted(false);//设置是否绘制边框
        btnRobot.setBorder(null);//设置边框

        btnOnline = new JButton("联机对战",buttonIcon);
        btnOnline.setHorizontalTextPosition(SwingConstants.CENTER);
        btnOnline.setOpaque(false);
        btnOnline.setContentAreaFilled(false);//设置图片填满按钮所在的区域
        btnOnline.setBorderPainted(false);//设置是否绘制边框
        btnOnline.setBorder(null);//设置边框

//        btnLogin = new JButton("登录");
//        btnLogout = new JButton("退出");
        //初始化退出按钮为隐藏
//        btnLogout.setEnabled(false);
        btnCoupe.setBounds(120,100,200,40);
        btnCoupe.setHorizontalAlignment(0);
        btnCoupe.setFont(new Font("微软雅黑",Font.BOLD,13));
        btnRobot.setBounds(120,150,200,40);
        btnRobot.setFont(new Font("微软雅黑",Font.BOLD,13));
        btnOnline.setBounds(120,200,200,40);
        btnOnline.setFont(new Font("微软雅黑",Font.BOLD,13));
//        btnLogin.setBounds(110,270,100,40);
//        btnLogin.setFont(new Font("微软雅黑",Font.BOLD,13));
//        btnLogout.setBounds(230,270,100,40);
//        btnLogout.setFont(new Font("微软雅黑",Font.BOLD,13));
        container.add(btnCoupe);
        container.add(btnRobot);
        container.add(btnOnline);
//        container.add(btnLogin);
//        container.add(btnLogout);

        String myName = myPlayer.getName();
        //欢迎下标
        if(myName!=null){
            welcome = new JLabel("欢迎您！" + myName);
            welcome.setBounds(160,300,100,20);
            container.add(welcome);
        }
        addListener();
    }


    // 界面显示，控件加载完毕后执行(向控件加载数据等)
    public static void init() {
        String myName = myPlayer.getName();
        //欢迎下标
        if(myName!=null){
            welcome = new JLabel("欢迎您！" + myName);
            welcome.setBounds(160,300,100,20);
            container.add(welcome);
//            btnLogin.setEnabled(false);
//            btnLogout.setEnabled(true);
        }
        chooseView.repaint();
    }

    public void addListener(){
//        //登录按钮
//        btnLogin.addActionListener(event -> {
//            LoginView loginView = new LoginView(this);
//            loginView.setVisible(true);
//        });
//        //退出按钮
//        btnLogout.addActionListener(event -> {
//            myPlayer.setName(null);
//            welcome.setVisible(false);
//            btnLogin.setEnabled(true);
//            btnLogout.setEnabled(false);
//        });
        //双人对战按钮
        btnCoupe.addActionListener(event -> {
            MainFrame mainFrame = new MainFrame();
            mainFrame.setVisible(true);
            MainFrame.init();
            GameCoupe.reStart();
//            this.dispose();
        });
        //人机对战按钮
        btnRobot.addActionListener(event -> {
            MainFrame mainFrame = new MainFrame();
            mainFrame.setVisible(true);
            MainFrame.init();
            GameRobot.reStart();
//            this.dispose();
        });
        //联机对战按钮
        btnOnline.addActionListener(event -> {
//            if(myPlayer.getName()==null){
//                MyDialog loginWarning = new MyDialog(chooseView,"请先登录");
//                loginWarning.setText("<br><br>您还未登录，<br><br>无法进行联机游戏！！");
//                loginWarning.setBtText("确定");
//                loginWarning.setVisible(true);
//            }else{
//                OnlineRoom onlineRoom = new OnlineRoom();
//                onlineRoom.setVisible(true);
//                OnlineRoom.init();
//            }


            OnlineRoom onlineRoom = new OnlineRoom();
            onlineRoom.setVisible(true);
            OnlineRoom.init();
        });

    }

}
